package com.ksv.qlsv;

import com.ksv.lop.*;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class LopHocActivity extends Activity {
	private ListView lv_lop_hoc;
	private LopHoc lop = null;
	private TextView txt_tim;
	private AddAdapter addAdapter;
	private Spinner com_khoa;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frmlophoc);   
        this.setTitle(getResources().getString(R.string.lop));
        lv_lop_hoc = (ListView)findViewById(R.id.lv_mon_hoc);
        txt_tim = (TextView)findViewById(R.id.txt_tim);
        com_khoa = (Spinner)findViewById(R.id.com_khoa);
        onItemClickListView();
        onItemLongClickListView();
        onTextChangedSearch();
        onItemSelectedChanged();
        loadListLopHoc();
        loadListKhoa();
        registerForContextMenu(lv_lop_hoc);               
    }
	
	private void onItemSelectedChanged() {
			com_khoa.setOnItemSelectedListener(new OnItemSelectedListener() {
				@Override
				public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) {
					try{
						GetData data = (GetData) com_khoa.getItemAtPosition(position);
						if(data.getID() == 0){
							loadListLopHoc();
						}
						else{
							loadListLopHoc_Khoa(data.getID());
						}
					}
					catch(Exception Ex){
						
					}
				}
				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
					
				}				
			});
	}

	private void loadListKhoa() {
		try{		    
			com_khoa.setAdapter(null);		       
    	    Cursor c = ConnectDB.getAllKhoa();
    	    KhoaAdapter.mID = ConnectDB.makhoa;
    	    KhoaAdapter.mName = ConnectDB.tenkhoa;
    	    KhoaAdapter khoaAdapter = new KhoaAdapter(this, c);
    	    com_khoa.setAdapter(khoaAdapter);
    	    com_khoa.setSelection(com_khoa.getCount() - 1);
    	}
    	catch(Exception ex){
    		
    	}
	}

	private void onTextChangedSearch() {
		// TODO Auto-generated method stub
		txt_tim.setOnKeyListener(new OnKeyListener() {			
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				// TODO Auto-generated method stub
				if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)){
					loadListLopHoc_Search(txt_tim.getText().toString());
				}
				return false;
			}
		});
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		loadListLopHoc();
		super.onResume();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inf = getMenuInflater();
		inf.inflate(R.menu.menu_add, menu);
		menu.getItem(0).setIcon(R.drawable.ic_launcher);
		menu.getItem(1).setIcon(R.drawable.ic_launcher);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getItemId() == R.id.m_new){			
			LopHocEditActivity.iAddEditView = 1;
			Intent intent = new Intent(LopHocActivity.this, LopHocEditActivity.class);
			startActivity(intent);
		}
		else if(item.getItemId() == R.id.m_delete){
			Intent intent = new Intent(this, LopHocDeleteActivity.class);
			startActivity(intent);
		}	
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub   
		MenuInflater inf = getMenuInflater();
    	inf.inflate(R.menu.contextmenu_update, menu);
    	menu.setHeaderTitle(lop.getTenLop());
		super.onCreateContextMenu(menu, v, menuInfo);		
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getItemId() == R.id.m_edit){									
			LopHocEditActivity.lop = lop;
			LopHocEditActivity.iAddEditView = 2;
			Intent intent = new Intent(LopHocActivity.this, LopHocEditActivity.class);
			startActivity(intent);	
		}
		else if(item.getItemId() == R.id.m_delete){
			AlertDialog diag = Alerts.ShowDeleteDialog(LopHocActivity.this);
			diag.setOnDismissListener(new OnDismissListener() {				
				@Override
				public void onDismiss(DialogInterface dialog) {
					// TODO Auto-generated method stub
					if(Alerts.bXoa == true){
						ConnectDB.deleteLopHoc(lop);
						loadListLopHoc();
					}
					dialog.dismiss();
				}
			});
			diag.show();
		}
		else if(item.getItemId() == R.id.m_view){
			LopHocEditActivity.lop = lop;
			LopHocEditActivity.iAddEditView = 3;
			Intent intent = new Intent(LopHocActivity.this, LopHocEditActivity.class);
			startActivity(intent);
		}
		return super.onContextItemSelected(item);
	}

	private void loadListLopHoc(){
		try{		    
			lv_lop_hoc.setAdapter(null);
    	    Cursor c = ConnectDB.getAllLopHoc();
    	    AddAdapter.mID = ConnectDB.malop;
    	    AddAdapter.mID_2 = ConnectDB.makhoa;
    	    AddAdapter.mName = ConnectDB.tenlop;
    	    addAdapter = new AddAdapter(this, c);
    	    lv_lop_hoc.setAdapter(addAdapter);
    	}
    	catch(Exception ex){
    		
    	}
	}
	
	private void loadListLopHoc_Search(String search){
		try{		    
		    lv_lop_hoc.setAdapter(null);
    	    Cursor c = ConnectDB.getAllLopHocSearch(search);
    	    AddAdapter.mID = ConnectDB.malop;
    	    AddAdapter.mID_2 = ConnectDB.makhoa;
    	    AddAdapter.mName = ConnectDB.tenlop;
    	    addAdapter = new AddAdapter(this, c);
    	    lv_lop_hoc.setAdapter(addAdapter);
    	}
    	catch(Exception ex){
    		
    	}
	}
	
	private void loadListLopHoc_Khoa(int makhoa){
		try{		    
		    lv_lop_hoc.setAdapter(null);
    	    Cursor c = ConnectDB.getAllLopHocKhoa(makhoa);
    	    AddAdapter.mID = ConnectDB.malop;
    	    AddAdapter.mID_2 = ConnectDB.makhoa;
    	    AddAdapter.mName = ConnectDB.tenlop;
    	    addAdapter = new AddAdapter(this, c);
    	    lv_lop_hoc.setAdapter(addAdapter);
    	}
    	catch(Exception ex){
    		
    	}
	}
	
	private void onItemLongClickListView(){
		lv_lop_hoc.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View v, int position,long id){
				// TODO Auto-generated method stub
        		try{
        			GetData data = (GetData) lv_lop_hoc.getItemAtPosition(position);
        			lop = new LopHoc();
       			 	lop.setMaLop(data.getID());
       			 	lop.setTenLop(data.getName());
       			 	lop.setMaKhoa(data.getID_2());
				}
				catch(Exception ex){
					
				}
				return false;
			}
		});
	}
		
	private void onItemClickListView(){
    	try{
    		lv_lop_hoc.setOnItemClickListener(new OnItemClickListener(){
	        	@Override
				public void onItemClick(AdapterView<?> parent, View v, int position,long id){
					// TODO Auto-generated method stub
	        		try{
	        			GetData data = (GetData) lv_lop_hoc.getItemAtPosition(position);
	        			lop = new LopHoc();
	       			 	lop.setMaLop(data.getID());
	       			 	lop.setTenLop(data.getName());
	       			 	lop.setMaKhoa(data.getID_2());
		       			LopHocEditActivity.lop = lop;
		     			LopHocEditActivity.iAddEditView = 3;
		     			Intent intent = new Intent(LopHocActivity.this, LopHocEditActivity.class);		     			
		     			startActivity(intent);
					}
					catch(Exception ex){
						
					}
				}			
    		}
    		);
        }
        catch(Exception ex){
        	
        }
    }
}
